package actionStruts;



import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import servise.TblAdvertAddressService;
import util.FileUp;
import vo.TblAdvertAddressVo;

import com.opensymphony.xwork2.ActionSupport;

public class TblAdvertAddressAction extends ActionSupport {
	private TblAdvertAddressVo tblAdvertAddressVo;
	private TblAdvertAddressVo tblAdvertAddressVoQuery;
	private List<TblAdvertAddressVo> tblAdvertAddressVoList;
	private String[] selectedId;
	private String buttonFlagValue;
	private File myFile;
	private String myFileFileName;
	private String myFileContentType;

	


	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public String getButtonFlagValue() {
		return buttonFlagValue;
	}

	public void setButtonFlagValue(String buttonFlagValue) {
		this.buttonFlagValue = buttonFlagValue;
	}

	public String[] getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String[] selectedId) {
		this.selectedId = selectedId;
	}

	public List<TblAdvertAddressVo> getTblAdvertAddressVoList() {
		return tblAdvertAddressVoList;
	}

	public void setTblAdvertAddressVoList(
			List<TblAdvertAddressVo> tblAdvertAddressVoList) {
		this.tblAdvertAddressVoList = tblAdvertAddressVoList;
	}

	public TblAdvertAddressVo getTblAdvertAddressVoQuery() {
		return tblAdvertAddressVoQuery;
	}

	public void setTblAdvertAddressVoQuery(
			TblAdvertAddressVo tblAdvertAddressVoQuery) {
		this.tblAdvertAddressVoQuery = tblAdvertAddressVoQuery;
	}

	public TblAdvertAddressVo getTblAdvertAddressVo() {
		return tblAdvertAddressVo;
	}

	public void setTblAdvertAddressVo(TblAdvertAddressVo tblAdvertAddressVo) {
		this.tblAdvertAddressVo = tblAdvertAddressVo;
	}



	public String execute(){
		String file = ServletActionContext.getServletContext().getRealPath("/uploadFile");
		File uploadFile = new File(file);
		String fileName = new Date().getTime()+myFileFileName.substring(myFileFileName.lastIndexOf("."));
		File myFileUp = new File(file + "/" + fileName);
		
		if(!uploadFile.exists()){
			uploadFile.mkdir();
		}
		if(myFile!=null){
			tblAdvertAddressVo.setDefPath(fileName);
			(new FileUp()).upLoadFile(myFile, myFileUp, 16*1024);
		}
		TblAdvertAddressService tblAdvertAddressService = new TblAdvertAddressService();
		tblAdvertAddressService.insertTblAdvertAddress(tblAdvertAddressVo);
		
		return "success";
	}
	
	public String search(){
		TblAdvertAddressService tblAdvertAddressService = new TblAdvertAddressService();
		tblAdvertAddressVoList = tblAdvertAddressService.findTblAdvertAddressList(tblAdvertAddressVoQuery);
		return "searchSucess";
	}
	
	public String searchByPage(){
		TblAdvertAddressService tblAdvertAddressService = new TblAdvertAddressService();
		tblAdvertAddressVoList = tblAdvertAddressService.findTblAdvertAddressByPageList(tblAdvertAddressVoQuery);
		
		return "searchSucess";
	}
	
	public String deleteAdvertAddress(){
		TblAdvertAddressService tblAdvertAddressService = new TblAdvertAddressService();
		try{
		tblAdvertAddressService.deltctAdvertAddress(selectedId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return search();
	}
	public String searchUpdateAdvertAddress(){
		buttonFlagValue = "upt";
		TblAdvertAddressService tblAdvertAddressService = new TblAdvertAddressService();
		tblAdvertAddressVo = tblAdvertAddressService.searchUpdateAdvertAddress(tblAdvertAddressVo);
		return "searchUpdateSucess";
	}
	public String uptAdvertAddress(){
		String file = ServletActionContext.getServletContext().getRealPath("/uploadFile");
		File uploadFile = new File(file);
		String fileName = new Date().toString()+myFileFileName.substring(myFileFileName.lastIndexOf("."));
		File myFileUp = new File(file + "/" + fileName);
		
		if(!myFile.exists()){
			myFile.mkdirs();
		}
		if(myFile!=null){
			tblAdvertAddressVo.setDefPath(myFileFileName);
			(new FileUp()).upLoadFile(myFile, myFileUp, 16*1024);
		}
		
		tblAdvertAddressVoQuery = tblAdvertAddressVo;
		try {
			TblAdvertAddressService tblAdvertAddressService = new TblAdvertAddressService();
			tblAdvertAddressService.uptTblAdvertAddress(tblAdvertAddressVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return search();
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}


}
